﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoderJoe;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

namespace CoderJoe
{
    class Program
    {
        static void Main(string[] args)
        {
            InstanceBuilder<ISimpleMath> builder = new InstanceBuilder<ISimpleMath>();

            builder.GenerateCode();

            CodeDomProvider provider = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
            CodeGeneratorOptions options = new CodeGeneratorOptions();

            provider.GenerateCodeFromType(builder.Declaration, Console.Out, options);

            CodeCompileUnit ccu = new CodeCompileUnit();
            CodeNamespace ns = new CodeNamespace("CoderJoe.Testing");
            ccu.Namespaces.Add(ns);

            ns.Types.Add(builder.Declaration);

            CompilerParameters compilerParms = new CompilerParameters();
            compilerParms.GenerateInMemory = false;
            compilerParms.OutputAssembly = Environment.CurrentDirectory + "\\Sample.dll";
            compilerParms.ReferencedAssemblies.Add("CoderJoe.Core.dll");
            compilerParms.ReferencedAssemblies.Add("CoderJoe.DisplayClass.exe");
            compilerParms.ReferencedAssemblies.Add("mscorlib.dll");
            compilerParms.ReferencedAssemblies.Add("System.Core.dll");

            CompilerResults result = provider.CompileAssemblyFromDom(compilerParms, ccu);
            if (result.Errors.HasErrors)
            {
                foreach (CompilerError error in result.Errors)
                {
                    Console.Error.WriteLine("Error:" + error.ToString());
                }

                Console.Read();
            }
        }
    }
}
